
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;

public class AmazonBookCatalogueTree {

    public static final String ROOT_UK_BOOKS_BROWSE_NODE_ID = "1025612";
    public static final String ROOT_US_BOOKS_BROWSE_NODE_ID = "1000";
    public static final String[][] TOP_LEVEL_BROWSE_NODE_INFO_US = {
        {"1", "Arts & Photography"},
        {"2", "Biographies & Memoirs"},
        {"3", "Business & Investing"},
        {"4", "Children's Books"},
        {"4366", "Comics & Graphic Novels"},
        {"5", "Computers & Internet"},
        {"6", "Cooking, Food & Wine"},
        {"86", "Entertainment"},
        {"301889", "Gay & Lesbian"},
        {"10", "Health, Mind & Body"},
        {"9", "History"},
        {"48", "Home & Garden"},
        {"10777", "Law"},
        {"17", "Literature & Fiction"},
        {"13996", "Medicine"},
        {"18", "Mystery & Thrillers"},
        {"53", "Nonfiction"},
        {"290060", "Outdoors & Nature"},
        {"20", "Parenting & Families"},
        {"21", "Reference"},
        {"23", "Romance"},
        {"25", "Science Fiction & Fantasy"},
        {"28", "Teens"},
        {"27", "Travel"},
        {"173507", "Professional & Technical"},
        {"22", "Religion & Spirituality"},
        {"75", "Science"},
        {"26", "Sports"}
    };
    static PrintWriter out;
    String browseNodeId = null;
    String name = null;
    boolean isCategoryRoot = false;

    @Override
    public String toString() {
        return name + "(" + browseNodeId + "," + isCategoryRoot + ")";
    }

    public static void main(String[] args) throws Exception {
        for (String[] bnInfo : TOP_LEVEL_BROWSE_NODE_INFO_US) {
            out = new PrintWriter(new FileWriter("c:\\Projects\\AmazonBooks\\catalogue\\" + bnInfo[1] + ".txt"));
            AmazonBookCatalogueTree tree = lookupBrowseNode(bnInfo[0], "");
            out.flush();
            out.close();
        }

    }
    ArrayList<AmazonBookCatalogueTree> children = new ArrayList<AmazonBookCatalogueTree>();

    public static AmazonBookCatalogueTree lookupBrowseNode(String aBrowseNodeId, String indent) {

        HashMap<String, String> params = new HashMap<String, String>();
        params.put("Service", "AWSECommerceService");
        params.put("Operation", "BrowseNodeLookup");
        params.put("Version", "2011-08-11");
        params.put("BrowseNodeId", aBrowseNodeId);
        params.put("AWSAccessKeyId", "AKIAJMSSTEDH23JSOXFQ");

        AWSAPICall apiCall = new AWSAPICall();
        Document domdoc = apiCall.getResponseDOM(params);

        Namespace ns = Namespace.getNamespace("http://webservices.amazon.com/AWSECommerceService/2011-04-01");

        Boolean isValid = Boolean.valueOf(domdoc.getRootElement().getChild("BrowseNodes", ns).getChild("Request", ns).getChildText("IsValid", ns));
        if (!isValid.booleanValue()) {
            return null;
        }

        Element browseNode = domdoc.getRootElement().getChild("BrowseNodes", ns).getChild("BrowseNode", ns);
        String browseNodeId = browseNode.getChildText("BrowseNodeId", ns);
        String name = browseNode.getChildText("Name", ns);
        String isCategoryRoot = browseNode.getChildText("IsCategoryRoot", ns);

        AmazonBookCatalogueTree tree = new AmazonBookCatalogueTree();
        tree.browseNodeId = browseNodeId;
        tree.name = name;
        tree.isCategoryRoot = "1".equals(isCategoryRoot);

        if (browseNode.getChild("Children", ns) != null) {
            List<Element> children = browseNode.getChild("Children", ns).getChildren("BrowseNode", ns);

            List<AmazonBookCatalogueTree> temp = new ArrayList<AmazonBookCatalogueTree>();
            for (Element child : children) {
                AmazonBookCatalogueTree c = new AmazonBookCatalogueTree();
                c.browseNodeId = child.getChildText("BrowseNodeId", ns);
                c.name = child.getChildText("Name", ns);
                c.isCategoryRoot = false;

                temp.add(c);
            }

            out.println(tree.browseNodeId + "\t" + indent + "\\" + tree.name + "\t" + temp.size());
            out.flush();
            for (AmazonBookCatalogueTree c : temp) {
                AmazonBookCatalogueTree nextTree = lookupBrowseNode(c.browseNodeId, indent + "\\" + tree.name);
                if (nextTree != null) {
                    tree.children.add(c);
                }
            }
        } else {
            out.println(tree.browseNodeId + "\t" + indent + "\\" + tree.name);
            out.flush();
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(AmazonBookCatalogueTree.class.getName()).log(Level.SEVERE, null, ex);
        }
        return tree;

    }
}
